﻿using System;
using SystemModule;

namespace M2Server
{
    /// <summary>
    /// 元宝寄售系统
    /// </summary>
    public partial class TPlayObject
    {
        // 交易成功后修改数据标识
        private unsafe void UpdateSellOffInfo(int code)
        {
            TDealOffInfo DealOffInfo;
            if (bo_YBDEAL) // 已开通元宝服务
            {
                for (int i = M2Share.sSellOffItemList.Count - 1; i >= 0; i--)
                {
                    if (M2Share.sSellOffItemList.Count <= 0)
                    {
                        break;
                    }
                    DealOffInfo = M2Share.sSellOffItemList[i];
                    if ((DealOffInfo != null))
                    {
                        if ((DealOffInfo.N == 2))
                        {
                            switch (code)
                            {
                                case 0:// 出售者
                                    if (DealOffInfo.sDealCharName == this.m_sCharName)
                                    {
                                        M2Share.sSellOffItemList.RemoveAt(i);
                                        Dispose(DealOffInfo);
                                        break;
                                    }
                                    break;
                                case 1:// 购买者
                                    if (DealOffInfo.sBuyCharName == this.m_sCharName)
                                    {
                                        M2Share.sSellOffItemList.RemoveAt(i);
                                        Dispose(DealOffInfo);
                                        break;
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 人物上线,检查是否有交易结束还没得到元宝
        /// </summary>
        private void GetSellOffGlod()
        {
            TDealOffInfo DealOffInfo;
            byte nCode = 0;
            if (m_boNotOnlineAddExp)  // 挂机则退出
            {
                return;
            }
            try
            {
                for (int i = M2Share.sSellOffItemList.Count - 1; i >= 0; i--)
                {
                    nCode = 1;
                    if (M2Share.sSellOffItemList.Count <= 0)
                    {
                        break;
                    }
                    nCode = 2;
                    DealOffInfo = M2Share.sSellOffItemList[i];
                    nCode = 3;
                    if ((DealOffInfo != null))
                    {
                        if ((string.Compare((DealOffInfo.sDealCharName).ToLower(), (this.m_sCharName).ToLower(), StringComparison.CurrentCultureIgnoreCase) == 0)
                            && (DealOffInfo.N == 1))
                        {
                            nCode = 4;
                            UpdateSellOffInfo(0);
                            nCode = 5;
                            DealOffInfo.N = 2;// 交易结束
                            nCode = 7;
                            m_nGameGold += DealOffInfo.nSellGold;
                            this.GameGoldChanged();
                            nCode = 8;
                            this.SysMsg(string.Format(GameMsgDef.sGetSellOffGlod, DealOffInfo.nSellGold), TMsgColor.c_Red, TMsgType.t_Hint);
                            nCode = 9;
                            if (M2Share.g_boGameLogGameGold)
                            {
                                M2Share.AddGameDataLog(string.Format(GameMsgDef.g_sGameLogMsg1, M2Share.LOG_GAMEGOLD, this.m_sMapName, this.m_nCurrX,
                                    this.m_nCurrY, this.m_sCharName, M2Share.g_Config.sGameGoldName,
                                    m_nGameGold, "寄售获得(" + (DealOffInfo.nSellGold).ToString() + ")",
                                    DealOffInfo.sBuyCharName));
                            }
                            break;
                        }
                    }
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TPlayObject.GetSellOffGlod Code:" + nCode);
            }
        }

        /// <summary>
        /// 查询玩家交易记录
        /// </summary>
        /// <returns></returns>
        public string SelectSellDate()
        {
            string result = "您未开通元宝寄售服务,请先开通！！！\\ \\<返回/@main>";
            TDealOffInfo DealOffInfo;
            if (bo_YBDEAL)// 已开通元宝服务
            {
                if (M2Share.sSellOffItemList.Count > 0)
                {
                    for (int i = 0; i < M2Share.sSellOffItemList.Count; i++)
                    {
                        DealOffInfo = M2Share.sSellOffItemList[i];
                        if ((DealOffInfo != null))
                        {
                            if (((DealOffInfo.sDealCharName).ToLower().CompareTo((this.m_sCharName).ToLower()) == 0)
                                && (DealOffInfo.N == 2))
                            {
                                result = "最后一笔出售记录:\\" + HUtil32.FormatDateTime("   yyyy年mm月dd日 hh时nn分", DealOffInfo.dSellDateTime) +
                                    ",\\  您与" + DealOffInfo.sBuyCharName + "交易成功,获得了" + DealOffInfo.nSellGold
                                    + "个" + M2Share.g_Config.sGameGoldName +
                                    "。\\ \\<返回/@main>";
                                return result;
                            }
                            else if (((DealOffInfo.sBuyCharName).ToLower().CompareTo((this.m_sCharName).ToLower()) == 0)
                                && ((DealOffInfo.N == 1) || (DealOffInfo.N == 2)))
                            {
                                result = "最后一笔购买记录:\\" + HUtil32.FormatDateTime("   yyyy年mm月dd日 hh时nn分", DealOffInfo.dSellDateTime) +
                                    ",\\  您与" + DealOffInfo.sDealCharName + "交易成功,支付了" + DealOffInfo.nSellGold + "个" +
                                    M2Share.g_Config.sGameGoldName + "。\\ \\<返回/@main>";
                                return result;
                            }
                        }
                    }
                }
                result = "您未进行任何寄售交易！！！\\ \\<返回/@main>";
            }
            return result;
        }

        /// <summary>
        /// 查询玩家是否操作过寄售
        /// </summary>
        /// <returns></returns>
        public bool SellOffInTime(int nCode)
        {
            bool result = false;
            TDealOffInfo DealOffInfo;
            if (M2Share.sSellOffItemList.Count > 0)
            {
                for (int i = 0; i < M2Share.sSellOffItemList.Count; i++)
                {
                    DealOffInfo = M2Share.sSellOffItemList[i];
                    if (DealOffInfo != null)
                    {
                        switch (nCode)
                        {
                            case 0:// 出售者
                                if (((DealOffInfo.sDealCharName).ToLower().CompareTo((this.m_sCharName).ToLower()) == 0) && ((DealOffInfo.N == 0) || (DealOffInfo.N == 3)))
                                {
                                    result = true;
                                    break;
                                }
                                break;
                            case 1:// 购买者
                                if (((DealOffInfo.sBuyCharName).ToLower().CompareTo((this.m_sCharName).ToLower()) == 0) && (DealOffInfo.N == 0))
                                {
                                    result = true;
                                    break;
                                }
                                break;
                        }
                    }
                }
            }
            return result;
        }
    }
}